package com.wuage.interfaces.permission;

import com.wuage.application.IUacPermissionService;
import com.wuage.domain.model.UacPermission;
import com.wuage.infrastructure.utils.BeanUtils;
import com.wuage.infrastructure.utils.Response;
import com.wuage.interfaces.permission.domain.CreateOrUpdatePermissionDO;
import com.wuage.interfaces.permission.vo.PermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Shynin
 * @version 1.0
 * @date 9/29/21 9:51 AM
 */
@RestController
@RequestMapping("/permission")
@Api(tags = "{权限}操作相关接口")
public class UacPermissionController {

    @Resource
    private IUacPermissionService uacPermissionService;

    @GetMapping("/")
    @ApiOperation("获取权限")
    public Response<List<PermissionVO>> get(@ApiParam("父权限id") @RequestParam(value = "parentId", required = false, defaultValue = "0") Long parentId) {
        List<UacPermission> permissions = uacPermissionService.select(parentId);
        return Response.success(BeanUtils.copyCollections(permissions, PermissionVO.class));
    }

    @PostMapping("/")
    @ApiOperation("创建权限")
    public Response<PermissionVO> create(@RequestBody @Valid CreateOrUpdatePermissionDO createOrUpdatePermissionDO) {
        UacPermission uacPermission = uacPermissionService.createOrUpdate(createOrUpdatePermissionDO);
        return Response.success(BeanUtils.copyProperties(uacPermission, PermissionVO.class));
    }

    @PutMapping("/")
    @ApiOperation("更新权限")
    public Response<PermissionVO> update(@RequestBody @Valid CreateOrUpdatePermissionDO createOrUpdatePermissionDO) {
        UacPermission uacPermission = uacPermissionService.createOrUpdate(createOrUpdatePermissionDO);
        return Response.success(BeanUtils.copyProperties(uacPermission, PermissionVO.class));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除权限")
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        Boolean result = uacPermissionService.delete(id);
        return Response.success(result);
    }
}
